<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>cuberenderer.cpp 示例文件 | Qt Quick 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtquick-index.html">Qt Quick</a></td><td ><a href="qtquick-rendercontrol-example.html">QQuickRenderControl Example</a></td><td >cuberenderer.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.1 参考指南</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">cuberenderer.cpp 示例文件</h1>
<span class="subtitle">rendercontrol/cuberenderer.cpp</span>
<!-- $$$rendercontrol/cuberenderer.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2017 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;cuberenderer.h&quot;</span>
  <span class="preprocessor">#include &lt;QOpenGLContext&gt;</span>
  <span class="preprocessor">#include &lt;QOpenGLFunctions&gt;</span>
  <span class="preprocessor">#include &lt;QOpenGLShaderProgram&gt;</span>
  <span class="preprocessor">#include &lt;QOpenGLVertexArrayObject&gt;</span>
  <span class="preprocessor">#include &lt;QOpenGLBuffer&gt;</span>
  <span class="preprocessor">#include &lt;QOpenGLVertexArrayObject&gt;</span>
  <span class="preprocessor">#include &lt;QOffscreenSurface&gt;</span>
  <span class="preprocessor">#include &lt;QWindow&gt;</span>

  CubeRenderer<span class="operator">::</span>CubeRenderer(<span class="type"><a href="../qtgui/qoffscreensurface.html">QOffscreenSurface</a></span> <span class="operator">*</span>offscreenSurface)
      : m_offscreenSurface(offscreenSurface)<span class="operator">,</span>
        m_context(nullptr)<span class="operator">,</span>
        m_program(nullptr)<span class="operator">,</span>
        m_vbo(nullptr)<span class="operator">,</span>
        m_vao(nullptr)<span class="operator">,</span>
        m_matrixLoc(<span class="number">0</span>)
  {
  }

  CubeRenderer<span class="operator">::</span><span class="operator">~</span>CubeRenderer()
  {
      <span class="comment">// Use a temporary offscreen surface to do the cleanup.</span>
      <span class="comment">// There may not be a native window surface available anymore at this stage.</span>
      m_context<span class="operator">-</span><span class="operator">&gt;</span>makeCurrent(m_offscreenSurface);

      <span class="keyword">delete</span> m_program;
      <span class="keyword">delete</span> m_vbo;
      <span class="keyword">delete</span> m_vao;

      m_context<span class="operator">-</span><span class="operator">&gt;</span>doneCurrent();
      <span class="keyword">delete</span> m_context;
  }

  <span class="type">void</span> CubeRenderer<span class="operator">::</span>init(<span class="type"><a href="../qtgui/qwindow.html">QWindow</a></span> <span class="operator">*</span>w<span class="operator">,</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span> <span class="operator">*</span>share)
  {
      m_context <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span>;
      m_context<span class="operator">-</span><span class="operator">&gt;</span>setShareContext(share);
      m_context<span class="operator">-</span><span class="operator">&gt;</span>setFormat(w<span class="operator">-</span><span class="operator">&gt;</span>requestedFormat());
      m_context<span class="operator">-</span><span class="operator">&gt;</span>create();
      <span class="keyword">if</span> (<span class="operator">!</span>m_context<span class="operator">-</span><span class="operator">&gt;</span>makeCurrent(w))
          <span class="keyword">return</span>;

      <span class="type"><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a></span> <span class="operator">*</span>f <span class="operator">=</span> m_context<span class="operator">-</span><span class="operator">&gt;</span>functions();
      f<span class="operator">-</span><span class="operator">&gt;</span>glClearColor(<span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.1f</span><span class="operator">,</span> <span class="number">0.25f</span><span class="operator">,</span> <span class="number">1.0f</span>);
      f<span class="operator">-</span><span class="operator">&gt;</span>glViewport(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> w<span class="operator">-</span><span class="operator">&gt;</span>width() <span class="operator">*</span> w<span class="operator">-</span><span class="operator">&gt;</span>devicePixelRatio()<span class="operator">,</span> w<span class="operator">-</span><span class="operator">&gt;</span>height() <span class="operator">*</span> w<span class="operator">-</span><span class="operator">&gt;</span>devicePixelRatio());

      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>vertexShaderSource <span class="operator">=</span>
          <span class="string">&quot;attribute highp vec4 vertex;\n&quot;</span>
          <span class="string">&quot;attribute lowp vec2 coord;\n&quot;</span>
          <span class="string">&quot;varying lowp vec2 v_coord;\n&quot;</span>
          <span class="string">&quot;uniform highp mat4 matrix;\n&quot;</span>
          <span class="string">&quot;void main() {\n&quot;</span>
          <span class="string">&quot;   v_coord = coord;\n&quot;</span>
          <span class="string">&quot;   gl_Position = matrix * vertex;\n&quot;</span>
          <span class="string">&quot;}\n&quot;</span>;
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>fragmentShaderSource <span class="operator">=</span>
          <span class="string">&quot;varying lowp vec2 v_coord;\n&quot;</span>
          <span class="string">&quot;uniform sampler2D sampler;\n&quot;</span>
          <span class="string">&quot;void main() {\n&quot;</span>
          <span class="string">&quot;   gl_FragColor = vec4(texture2D(sampler, v_coord).rgb, 1.0);\n&quot;</span>
          <span class="string">&quot;}\n&quot;</span>;
      m_program <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglshaderprogram.html">QOpenGLShaderProgram</a></span>;
      m_program<span class="operator">-</span><span class="operator">&gt;</span>addCacheableShaderFromSourceCode(<span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span><span class="operator">::</span>Vertex<span class="operator">,</span> vertexShaderSource);
      m_program<span class="operator">-</span><span class="operator">&gt;</span>addCacheableShaderFromSourceCode(<span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span><span class="operator">::</span>Fragment<span class="operator">,</span> fragmentShaderSource);
      m_program<span class="operator">-</span><span class="operator">&gt;</span>bindAttributeLocation(<span class="string">&quot;vertex&quot;</span><span class="operator">,</span> <span class="number">0</span>);
      m_program<span class="operator">-</span><span class="operator">&gt;</span>bindAttributeLocation(<span class="string">&quot;coord&quot;</span><span class="operator">,</span> <span class="number">1</span>);
      m_program<span class="operator">-</span><span class="operator">&gt;</span>link();
      m_matrixLoc <span class="operator">=</span> m_program<span class="operator">-</span><span class="operator">&gt;</span>uniformLocation(<span class="string">&quot;matrix&quot;</span>);

      m_vao <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a></span>;
      m_vao<span class="operator">-</span><span class="operator">&gt;</span>create();
      <span class="type"><a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a></span><span class="operator">::</span>Binder vaoBinder(m_vao);

      m_vbo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglbuffer.html">QOpenGLBuffer</a></span>;
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>create();
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>bind();

      GLfloat v<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>

          <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span><span class="number">0.5</span><span class="operator">,</span>

          <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>
          <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>
          <span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span>  <span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span>  <span class="operator">-</span><span class="number">0.5</span><span class="operator">,</span> <span class="number">0.5</span>
      };
      GLfloat texCoords<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span>
          <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span>
          <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span>
          <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span>

          <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span>
          <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span>
          <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span>
          <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span>

          <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span>
          <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span>
          <span class="number">1.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span>
          <span class="number">0.0f</span><span class="operator">,</span><span class="number">1.0f</span><span class="operator">,</span> <span class="number">0.0f</span><span class="operator">,</span><span class="number">0.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span><span class="number">1.0f</span>
      };

      <span class="keyword">const</span> <span class="type">int</span> vertexCount <span class="operator">=</span> <span class="number">36</span>;
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>allocate(<span class="keyword">sizeof</span>(GLfloat) <span class="operator">*</span> vertexCount <span class="operator">*</span> <span class="number">5</span>);
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>write(<span class="number">0</span><span class="operator">,</span> v<span class="operator">,</span> <span class="keyword">sizeof</span>(GLfloat) <span class="operator">*</span> vertexCount <span class="operator">*</span> <span class="number">3</span>);
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>write(<span class="keyword">sizeof</span>(GLfloat) <span class="operator">*</span> vertexCount <span class="operator">*</span> <span class="number">3</span><span class="operator">,</span> texCoords<span class="operator">,</span> <span class="keyword">sizeof</span>(GLfloat) <span class="operator">*</span> vertexCount <span class="operator">*</span> <span class="number">2</span>);
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>release();

      <span class="keyword">if</span> (m_vao<span class="operator">-</span><span class="operator">&gt;</span>isCreated())
          setupVertexAttribs();
  }

  <span class="type">void</span> CubeRenderer<span class="operator">::</span>resize(<span class="type">int</span> w<span class="operator">,</span> <span class="type">int</span> h)
  {
      m_proj<span class="operator">.</span>setToIdentity();
      m_proj<span class="operator">.</span>perspective(<span class="number">45</span><span class="operator">,</span> w <span class="operator">/</span> <span class="type">float</span>(h)<span class="operator">,</span> <span class="number">0.01f</span><span class="operator">,</span> <span class="number">100.0f</span>);
  }

  <span class="type">void</span> CubeRenderer<span class="operator">::</span>setupVertexAttribs()
  {
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>bind();
      m_program<span class="operator">-</span><span class="operator">&gt;</span>enableAttributeArray(<span class="number">0</span>);
      m_program<span class="operator">-</span><span class="operator">&gt;</span>enableAttributeArray(<span class="number">1</span>);
      m_context<span class="operator">-</span><span class="operator">&gt;</span>functions()<span class="operator">-</span><span class="operator">&gt;</span>glVertexAttribPointer(<span class="number">0</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> GL_FLOAT<span class="operator">,</span> GL_FALSE<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> nullptr);
      m_context<span class="operator">-</span><span class="operator">&gt;</span>functions()<span class="operator">-</span><span class="operator">&gt;</span>glVertexAttribPointer(<span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> GL_FLOAT<span class="operator">,</span> GL_FALSE<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span>
                                                    (<span class="keyword">const</span> <span class="type">void</span> <span class="operator">*</span>)(<span class="number">36</span> <span class="operator">*</span> <span class="number">3</span> <span class="operator">*</span> <span class="keyword">sizeof</span>(GLfloat)));
      m_vbo<span class="operator">-</span><span class="operator">&gt;</span>release();
  }

  <span class="type">void</span> CubeRenderer<span class="operator">::</span>render(<span class="type"><a href="../qtgui/qwindow.html">QWindow</a></span> <span class="operator">*</span>w<span class="operator">,</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span> <span class="operator">*</span>share<span class="operator">,</span> <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> texture)
  {
      <span class="keyword">if</span> (<span class="operator">!</span>m_context)
          init(w<span class="operator">,</span> share);

      <span class="keyword">if</span> (<span class="operator">!</span>m_context<span class="operator">-</span><span class="operator">&gt;</span>makeCurrent(w))
          <span class="keyword">return</span>;

      <span class="type"><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a></span> <span class="operator">*</span>f <span class="operator">=</span> m_context<span class="operator">-</span><span class="operator">&gt;</span>functions();
      f<span class="operator">-</span><span class="operator">&gt;</span>glClear(GL_COLOR_BUFFER_BIT <span class="operator">|</span> GL_DEPTH_BUFFER_BIT);

      <span class="keyword">if</span> (texture) {
          f<span class="operator">-</span><span class="operator">&gt;</span>glBindTexture(GL_TEXTURE_2D<span class="operator">,</span> texture);
          f<span class="operator">-</span><span class="operator">&gt;</span>glFrontFace(GL_CW); <span class="comment">// because our cube's vertex data is such</span>
          f<span class="operator">-</span><span class="operator">&gt;</span>glEnable(GL_CULL_FACE);
          f<span class="operator">-</span><span class="operator">&gt;</span>glEnable(GL_DEPTH_TEST);

          m_program<span class="operator">-</span><span class="operator">&gt;</span>bind();
          <span class="type"><a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a></span><span class="operator">::</span>Binder vaoBinder(m_vao);
          <span class="comment">// If VAOs are not supported, set the vertex attributes every time.</span>
          <span class="keyword">if</span> (<span class="operator">!</span>m_vao<span class="operator">-</span><span class="operator">&gt;</span>isCreated())
              setupVertexAttribs();

          <span class="keyword">static</span> GLfloat angle <span class="operator">=</span> <span class="number">0</span>;
          QMatrix4x4 m;
          m<span class="operator">.</span>translate(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">2</span>);
          m<span class="operator">.</span>rotate(<span class="number">90</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
          m<span class="operator">.</span>rotate(angle<span class="operator">,</span> <span class="number">0.5</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>);
          angle <span class="operator">+</span><span class="operator">=</span> <span class="number">0.5f</span>;

          m_program<span class="operator">-</span><span class="operator">&gt;</span>setUniformValue(m_matrixLoc<span class="operator">,</span> m_proj <span class="operator">*</span> m);

          <span class="comment">// Draw the cube.</span>
          f<span class="operator">-</span><span class="operator">&gt;</span>glDrawArrays(GL_TRIANGLES<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">36</span>);
      }

      m_context<span class="operator">-</span><span class="operator">&gt;</span>swapBuffers(w);
  }

</pre>
</div>
<!-- @@@rendercontrol/cuberenderer.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
